package com.ruoyi.api.group;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.CrmGroup;
import com.ruoyi.system.service.ICrmGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/cgroup")
public class CGroupController {

    @Autowired
    private ICrmGroupService crmGroupServiceImpl;

    @PostMapping("/insertGroup")
    public AjaxResult insertGroup(@RequestBody CrmGroup crmGroup){
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        crmGroup.setCreateUser(userId);
       return  AjaxResult.success(crmGroupServiceImpl.insertCrmGroup(crmGroup));
    }

    @PostMapping("/updateGroup")
    public AjaxResult updateGroup(@RequestBody CrmGroup crmGroup){
        return AjaxResult.success(crmGroupServiceImpl.updateCrmGroup(crmGroup));
    }

    @PostMapping("/deleteGroup")
    public AjaxResult deleteGroup(Long id){
        return AjaxResult.success(crmGroupServiceImpl.deleteCrmGroupById(id));
    }

    @GetMapping("/selectGroup")
    public AjaxResult selectGroup(){
        CrmGroup crmGroup=new CrmGroup();
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        crmGroup.setCreateUser(userId);
        return AjaxResult.success(crmGroupServiceImpl.selectCrmGroupList(crmGroup));
    }

    /**
     * 查看分组及分组的客户数量
     * @return
     */
    @GetMapping("/selectGroupAndCount")
    public AjaxResult selectGroupAndCount(){
         Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
         return AjaxResult.success(crmGroupServiceImpl.selectCrmGroupCount(userId));
    }
}
